﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaAttribute;
using Csla.Wpf;
using OpenExpressApp.Command;
using Csla.Core;
using System.ComponentModel;
using System.Windows;
using OpenExpressApp.Template;
using OpenExpressApp.Types;
using Csla;
using OpenExpressApp.WPF.Controls;
using System.Collections.ObjectModel;
using log4net;
using System.Collections;
using OpenExpressApp.MetaModel;
using Avalon.Windows.Controls;
using OpenExpressApp.WPF.Templete;
using OpenExpressApp.Module;
using OpenExpressApp.Module.WPF;
using System.Windows.Controls;
using Ricciolo.Controls;
using System.Reflection;
using System.Windows.Markup;
using System.Diagnostics;
using AvalonDock;
using OpenExpressApp.Module.WPF.Editors;
using Csla.OpenExpressApp;

namespace OpenExpressApp.WPF.Command
{
    /// <summary>
    /// 删除一个根对象
    /// </summary>
    [Command(CommandNames.DeleteBillObject, ImageName = "Delete.bmp", Label = "删除", ToolbarType = ToolbarType.Main, ModuleType = ModuleType.Bill, ToolTip = "删除记录")]
    public class DeleteBillObject : WPFListViewCommand
    {
        public override bool CanExecute(ListObjectView view)
        {
            if (view == null)
            {
                return false;
            }
            if (base.CanExecute(view) == false)
            {
                return false;
            }
            var bindingList = view.Data as IBindingList;
            if (bindingList == null)//|| bindingList.AllowRemove == false)
            {
                return false;
            }
            return view.CurrentObject != null;
        }
        public override void Execute(ListObjectView view)
        {
            var result = TaskDialog.Show("删除后自动保存并不能取消，是否删除当前记录?", "确认删除", TaskDialogButtons.Yes | TaskDialogButtons.No);
            if (result != null && result.ButtonData != null && result.ButtonData.Button == TaskDialogButtons.Yes)
            {
                //获取选中对象的副本（使用副本是因为下面的操作，会改变选中的对象列表。）
                IList deleteList = view.SelectedObjects.OfType<object>().ToArray();
                int deleteCount = deleteList.Count;
                var dataList = view.Data as IBindingList;
                //删除完毕后，需要选中的元素
                object nextFocusItem = null;

                #region 打到nextFocusItem

                if (dataList.Count > deleteCount)
                {
                    var lastDeleteItem = deleteList[deleteCount - 1];
                    int index = dataList.IndexOf(lastDeleteItem);

                    //如果前面有不被删除的结点，则向前找
                    if (index >= deleteCount)
                    {
                        #region 向前找

                        for (int i = index - 1; i >= 0; i--)
                        {
                            var item = dataList[i];

                            bool notDelete = true;
                            for (int j = deleteCount - 1; j >= 0; j--)
                            {
                                if (deleteList[j] == item)
                                {
                                    notDelete = false;
                                    break;
                                }
                            }

                            if (notDelete)
                            {
                                nextFocusItem = item;
                                break;
                            }
                        }

                        #endregion
                    }
                    else
                    {
                        //后面一个
                        nextFocusItem = dataList[index + 1];
                    }
                }

                #endregion

                #region 全部删除

                for (int i = deleteCount - 1; i >= 0; i--)
                {
                    BusinessBase item = deleteList[i] as BusinessBase;
                   // item.ApplyEdit();

                    //如果是树形,则级联删除所有子节点
                    if (item is ITreeNode)
                    {
                        var children = (item as ITreeNode).GetRecurChildren();
                        //先删除细记录再删除主记录
                        for (int j = children.Count - 1; j >= 0; j--)
                        {
                            var child = children[j] as BusinessBase;
                            dataList.Remove(child);
                            child.Delete();

                            //如果不是新增的,则保存对象
                            if (child.IsNew == false)
                            {
                                (child as ISavable).Save();
                            }

                            //删除根节点对象集合
                            var parent = (child as ITreeNode).ParentNode;
                            if (null == parent)
                            {
                                (view.Control as MultiObjectTreeView).Roots.Remove(child);
                            }
                        }
                    }
                    else
                    {
                        dataList.Remove(item);
                        item.Delete();

                        //如果不是新增的,则保存对象
                        if (item.IsNew == false)
                        {
                            (item as ISavable).Save();
                        }
                    }
                }

                #endregion

                view.CurrentObject = nextFocusItem;
            }
        }
    }
}
